package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.bo.PermissionsBo;
import com.cskaoyan.model.admin.vo.*;
import com.cskaoyan.service.admin.SystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 19:54
 */
@RestController
@RequestMapping("admin/role")
public class SystemRoleController {

    @Autowired
    SystemRoleService systemRoleService;

    @RequestMapping("/options")
    public BaseRespVo roleOption() {
        BaseDataListVo baseData = systemRoleService.getRoles();
        return BaseRespVo.ok(baseData);
    }

    @RequestMapping("/list")
    public BaseRespVo roleList(BaseParam baseParam, String name) {
        BaseDataListVo baseData = systemRoleService.roleList(baseParam, name);
        return BaseRespVo.ok(baseData);
    }

    @RequestMapping("/create")
    public BaseRespVo roleCreate(@RequestBody RoleCreateVo roleCreateVo) {
        MarketRole marketRole = systemRoleService.roleCreate(roleCreateVo);
        return BaseRespVo.ok(marketRole);
    }

    @RequestMapping("/update")
    public BaseRespVo roleUpdate(@RequestBody MarketRole marketRole) {
        try {
            systemRoleService.roleUpdate(marketRole);
        } catch (IllegalArgumentException e) {
            return BaseRespVo.fail(502, "系统内部错误");
        }
        return BaseRespVo.ok(null);
    }

    @RequestMapping("/delete")
    public BaseRespVo roleDelete(@RequestBody MarketRole marketRole) {
//        if (marketRole.getId()==1||marketRole.getId()==2||marketRole.getId()==3){
//            return BaseRespVo.fail(642,"当前角色存在管理员，不能删除");
//        }
        try {
            systemRoleService.deleteRoles(marketRole);
        } catch (IllegalArgumentException e){
            return BaseRespVo.fail(642,"当前角色存在管理员，不能删除");
        }
        return BaseRespVo.ok(null);
    }


    @GetMapping ("/permissions")
    public BaseRespVo rolePermissions(Integer roleId){
        PermissionsVo permissionsVo=systemRoleService.rolePermissions(roleId);
        return BaseRespVo.ok(permissionsVo);
    }

    @PostMapping("/permissions")
    public BaseRespVo rolePostPermissions(@RequestBody PermissionsBo permissionsBo){
        systemRoleService.updatePermissions(permissionsBo);
        return BaseRespVo.ok(null);
    }
}

